<template>
  <!-- 评教管理 -->
  <div>
    <div class="edu-page-warp">
      <condition-card @reset="reset" @search="searchAll" :label-width="labelWidth" style="margin-bottom: 20px;">
        <el-row :gutter="24">
          <!-- 学院 -->
          <el-col :span="8">
            <el-form-item label="学院">
              <DictSelect
                labelAll="请选择"
                :disabled="facultyDisable"
                filterable multiple collapse-tags
                v-model="queryParams.facultyList"
                :data="options.faculty"
                @change="handleChangeFaculty"
              ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 校区 -->
          <el-col :span="8">
            <el-form-item :label="$t('校区')">
              <dict-select
                v-model="queryParams.campusList"
                label-all="请选择"
                multiple
                :data="options.campusList">
              </dict-select>
            </el-form-item>
          </el-col>
          <!-- 年级 -->
          <el-col :span="8">
            <el-form-item label="年级">
              <el-select v-model="queryParams.gradeList" multiple
                collapse-tags filterable clearable style="width: 100%">
                <el-option v-for="item in options.grade" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
          <!-- 专业 -->
          <!-- <el-col :span="8">
            <el-form-item :label="$t('label.major')">
              <EduMajorBkSelect v-model="queryParams.professionList" :multiple="true"
                :faculty="queryParams.facultyList"
                :grade="queryParams.gradeList"></EduMajorBkSelect>
            </el-form-item>
          </el-col> -->
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="专业">
                <DictSelect
                    labelAll="请选择"
                    v-model="queryParams.professionList"
                    :data="options.profession"
                    filterable
                    multiple
                    collapse-tags
                    clearable
                ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 培养类别 -->
          <el-col :span="8">
            <el-form-item :label="$t('label.trainingCategory')">
              <el-select v-model="queryParams.trainingCategoryList"
                filterable multiple collapse-tags
                clearable style="width: 100%;">
                <el-option
                  v-for="item in options.trainingCategory"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 培养层次 -->
          <el-col :span="8">
            <el-form-item :label="$t('label.trainingLevel')" prop="trainingLevel">
              <dict-select
                labelAll="请选择"
                multiple collapse-tags
                v-model="queryParams.trainingLevelList"
                :data="options.trainingLevelList"
                :show-all="false"
                clearable
                @change="getCourseCodeList"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <!-- 学位类型 -->
          <el-col :span="8">
            <el-form-item label="学位类型">
              <el-select v-model="queryParams.degreeTypeList"
                multiple clearable collapse-tags style="width: 100%">
                <el-option v-for="item in options.degreeType" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 学习形式 -->
          <el-col :span="8">
            <el-form-item label="学习形式">
              <DictSelect
                multiple collapse-tags
                labelAll="请选择"
                v-model="queryParams.formLearningList"
                :data="options.formLearning"
              ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 入学季节 -->
          <el-col :span="8">
            <el-form-item label="入学季节" prop="enrolSeason">
              <el-select
                v-model="queryParams.enrolSeasonList"
                multiple
                collapse-tags
                style="width: 100%"
                clearable>
                <el-option
                  v-for="item in options.enrolSeason"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <!-- 学号/姓名 -->
          <el-col :span="8">
            <el-form-item label="学号/姓名" prop="enrolSeason">
              <el-input v-model="queryParams.studentIdOrName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>

      <!-- 表格card -->
      <el-card shadow="never">
        <div class="edu-opertion-bar">
          <div class="float-left edu-page-title">{{titleDone}}</div>
          <div class="float-right">
            <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
              <el-button type="danger" @click="deleteObjectClick">{{$t('移除')}}</el-button>
            </el-row>
          </div>
        </div>
        <!-- 主页表格 -->
        <edu-table :url="doneUrl" ref="doneTable" :order="true" :selection="true">
          <edu-table-column :label="$t('学院')" prop="facultyName"></edu-table-column>
          <edu-table-column :label="$t('年级')" prop="grade"></edu-table-column>
          <edu-table-column :label="$t('培养层次')" prop="trainingLevelName"></edu-table-column>
          <edu-table-column :label="$t('专业')" prop="professionName"></edu-table-column>
          <edu-table-column :label="$t('校区')" prop="campusName"></edu-table-column>
          <edu-table-column :label="$t('入学季节')" prop="enrolSeasonName"></edu-table-column>
          <edu-table-column :label="$t('培养类别')" prop="trainingCategoryName"></edu-table-column>
          <edu-table-column :label="$t('学位类型')" prop="degreeTypeName"></edu-table-column>
          <edu-table-column :label="$t('学习形式')" prop="fromLearningName"></edu-table-column>
          <edu-table-column :label="$t('学号')" prop="studentId"></edu-table-column>
          <edu-table-column :label="$t('姓名')" prop="studentName"></edu-table-column>
        </edu-table>
      </el-card>

      <!-- 表格card -->
      <el-card shadow="never">
        <div class="edu-opertion-bar">
          <div class="float-left edu-page-title">{{titleNot}}</div>
          <div class="float-right">
            <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
              <el-button type="primary" @click="save">{{$t('添加')}}</el-button>
            </el-row>
          </div>
        </div>
        <!-- 主页表格 -->
        <edu-table :url="url" ref="mainTable" :order="true" :selection="true">
          <edu-table-column :label="$t('学院')" prop="facultyName"></edu-table-column>
          <edu-table-column :label="$t('年级')" prop="grade"></edu-table-column>
          <edu-table-column :label="$t('培养层次')" prop="trainingLevelName"></edu-table-column>
          <edu-table-column :label="$t('专业')" prop="professionName"></edu-table-column>
          <edu-table-column :label="$t('校区')" prop="campusName"></edu-table-column>
          <edu-table-column :label="$t('入学季节')" prop="enrolSeasonName"></edu-table-column>
          <edu-table-column :label="$t('培养类别')" prop="trainingCategoryName"></edu-table-column>
          <edu-table-column :label="$t('学位类型')" prop="degreeTypeName"></edu-table-column>
          <edu-table-column :label="$t('学习形式')" prop="fromLearningName"></edu-table-column>
          <edu-table-column :label="$t('学号')" prop="studentId"></edu-table-column>
          <edu-table-column :label="$t('姓名')" prop="studentName"></edu-table-column>
        </edu-table>
        <el-row :gutter="20" style="text-align: right;margin-top: 20px;">
          <el-button type="primary" @click="prevStep">{{$t('上一步')}}</el-button>
          <el-button @click="cancel">{{$t('返回')}}</el-button>
        </el-row>
      </el-card>
    </div>
  </div>
</template>
<script>
import UTILS from "common/src/utils/utils";
import baseUrl from 'common/src/api/teachingEvaluation/env'
import { queryDic } from "common/src/api/dictionary"
import processEvaluationApi from 'common/src/api/teachingEvaluation/processEvaluation'
import courseEvaluationApi from 'common/src/api/teachingEvaluation/courseEvaluation'
export default {
  props: {
    ifEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const getGrade = (function() {
      let now = new Date().getFullYear();
      let years = [];
      for (let i = 0; i < 18; i++) {
        years.push({
          label: now - i,
          value: now - i
        });
      }
      return years;
    })();
    return {
      routeParams: {},
      titleNot: '未配置对象列表',
      titleDone: '已配置对象列表',
      url: `${baseUrl}/processStudent/object/getPageList`,
      doneUrl: `${baseUrl}/processStudent/object/look/getPageList`,
      labelWidth: '120px',
      currentCalendarId: '',
      queryParams: {
        managementId: '',
        campusList: [],
        gradeList: [],
        enrolSeasonList: [],
        trainingLevelList: [],
        facultyList: [],
        trainingCategoryList: [],
        degreeTypeList: [],
        professionList: [],
        formLearningList: [],
        managementName: '',
        studentIdOrName: ''
      },
      options: {
        campusList: [], // 校区
        grade: getGrade, // 年级
        trainingLevelList: [], // 培养层次
        trainingCategory: [], // 培养类别
        degreeType: [], // 学位类型
        formLearning: [], // 学习形式
        enrolSeason: [], // 入学季节
        profession: [], // 专业
        faculty: []
      },
      facultyList: [],
      dataList: [],
      facultyDisable: false
    }
  },
  created() {
    if (sessionStorage.getItem('currFacultyCode')) {
      this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      this.facultyDisable = true
    }
    this.handleChangeFaculty()
    this.getOptions();
  },
  mounted() {
    if (this.$route.query.managementId) {
      this.queryParams.managementId = this.$route.query.managementId;
      this.queryParams.calendarId = this.$route.query.calendarId;
      this.searchAll()
    }
  },
  methods: {
    // 查询两个表格数据
    searchAll() {
      this.searchList()
      this.searchDoneList()
    },
    // 查询
    searchList() {
      this.$refs.mainTable.pageNum = 1;
      this.$refs.mainTable.search(this.queryParams)
    },
    searchDoneList() {
      this.$refs.doneTable.pageNum = 1;
      this.$refs.doneTable.search(this.queryParams)
    },
    // 重置查询参数
    reset() {
      this.queryParams = {
        managementId: this.$route.query.managementId ? this.$route.query.managementId : this.routeParams.managementId,
        calendarId: this.$route.query.calendarId ? this.$route.query.calendarId : this.routeParams.calendarId,
        campusList: [],
        gradeList: [],
        enrolSeasonList: [],
        trainingLevelList: [],
        facultyList: [],
        trainingCategoryList: [],
        degreeTypeList: [],
        professionList: [],
        formLearningList: [],
        managementName: '',
        studentIdOrName: ''
      };
      if (this.facultyDisable) {
        this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      }
      this.searchAll();
    },
    // 获取字典数据
    async getOptions() {
      //筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: [
          "X_XQ", // 校区
          "X_PYLB", // 培养类别
          "X_PYCC", // 培养层次
          "X_XWLX", // 学位类型
          "X_XXXS", // 学习形式
          "X_RXJJ", // 入学季节
          "X_YX"
        ]
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            campusList: "X_XQ",
            // majors:"G_ZY",
            trainingCategory: "X_PYLB", // 培养类别
            trainingLevelList: "X_PYCC", // 培养层次
            degreeType: "X_XWLX", // 学位类型
            formLearning: "X_XXXS", // 学习形式
            enrolSeason: "X_RXJJ",
            faculty: "X_YX"
          },
          res.data,
          this.options
        );
      });
    },
    // 取消 直接返回列表页
    cancel() {
      this.$emit('onlyGoBack')
      this.$router.push({
        query: {}
      })
    },
    // 上一步 返回上一页
    prevStep() {
      if (this.ifEdit) {
        this.$emit('editGoBack')
      } else {
        this.$emit('goToAddOrEdit')
      }
    },
    // 对象配置最后一次保存
    save() {
      const checkDatas = this.$refs.mainTable.getCheckData();
      let param = Object.assign({},this.queryParams)
      param.objectList = JSON.parse(JSON.stringify(checkDatas))
      processEvaluationApi.objectStudentProcessEvaSetConfirm(param).then(res => {
        if (res.code == 200) {
          this.$message.success(this.$t('新增过程评价对象配置成功，请稍后发布'))
          this.searchAll();
        }
      })
    },
    // 删除对象配置
    deleteObjectClick() {
      this.$confirm(this.$t('确定要移除已配置的这些对象吗？'), this.$t("message.prompt"), {
        confirmButtonText: this.$t("message.confirm"),
        cancelButtonText: this.$t("message.cancel"),
        type: "warning"
      }).then(() => {
        const checkDatas = this.$refs.doneTable.getCheckData();
        let param = Object.assign({},this.queryParams)
        param.objectList = JSON.parse(JSON.stringify(checkDatas))
        processEvaluationApi.objectStudentProcessEvaSetDelete(param).then(res => {
          if (res.code == 200) {
            this.$message.success(this.$t('对象删除成功，请稍后发布'))
            this.searchAll()
          }
        })
      })
    },
    // 根据学院获取专业
    handleChangeFaculty(val) {
      this.options.profession = [];
      this.queryParams.professionList = [];
      // this.query.currentProfession = "";
      let facultys, degreeType;
      facultys = this.queryParams.facultyList;
      // degreeType = this.queryParams.degreeTypeList ? [this.queryParams.degreeTypeList] : [];
      const data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: ["G_ZY"],
        filter: {
          prefix: facultys,
          specilaCode: "XY2ZY",
          facultys: facultys,
          // grade: this.queryParams.gradeList,
          // degreeType: degreeType
        }
      };
      queryDic(data).then(res => {
        if (res.code === 200)
          UTILS.fillSelect(
            {
              profession: "G_ZY"
            },
            res.data,
            this.options
          );
      });
    }
  },
  watch: {
    '$route'(to, from) {
        if (Object.values(to.query).length > 0) {
          this.routeParams = _.cloneDeep(to.query)
          if (this.routeParams.managementId) {
            this.queryParams.managementId = this.routeParams.managementId;
            this.queryParams.calendarId = this.routeParams.calendarId;
            this.searchAll()
          }
        }
    }
  }
}
</script>
<style lang="scss">
</style>
